package com.boss.salary.service.impl;

import com.boss.common.entity.PageDTO;
import com.boss.common.util.DateUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.constant.SystemDictConstant;
import com.boss.salary.dao.IAgencyInfoDao;
import com.boss.salary.dao.IOperrateLogsDao;
import com.boss.salary.dao.IPersonInfoDao;
import com.boss.salary.entity.AgencyInfo;
import com.boss.salary.entity.OperrateLogs;
import com.boss.salary.service.IOperrateLogsService;
import com.boss.sys.utils.SysUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class OperrateLogsServiceImpl implements IOperrateLogsService{

    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(OperrateLogsServiceImpl.class);

    @Autowired
    private IOperrateLogsDao iOperrateLogsDao;

    @Autowired
    private IPersonInfoDao iPersonInfoDao;

    @Autowired
    private IAgencyInfoDao iAgencyInfoDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOperrateLogs(OperrateLogs operrateLogs) throws Exception{
        //id
        operrateLogs.setId(SysUtils.generateGuid());
        //创建时间
        operrateLogs.setCreateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        //修改时间
        operrateLogs.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        //设置未删除
        operrateLogs.setIsDeleted(SystemDictConstant.IS_FALSE);
        //设置年份
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        operrateLogs.setYear(String.valueOf(year));

        iOperrateLogsDao.saveOperrateLogs(operrateLogs);
    }

    @Override
    public PageDTO<OperrateLogs> queryOperatelogsPage(PaginationDTO paginationDTO, Map<String, Object> queryParam, List<String> childrenId) {
        List<OperrateLogs> operrateLogs = iOperrateLogsDao.queryOperatelogsPage(paginationDTO, queryParam, childrenId);
        for (OperrateLogs operrateLog : operrateLogs) {
            //将操作人单位转化名称
            if(!StringUtils.isEmpty(operrateLog.getUserAgencyId())){
                AgencyInfo agencyInfo = iAgencyInfoDao.getNameById(operrateLog.getUserAgencyId());
                operrateLog.setUserAgencyId(agencyInfo.getAgencyName());
            }
            if(!StringUtils.isEmpty(operrateLog.getAgencyId())){
                //将相关单位字符串转化集合
                List<String> agencyIds = Arrays.asList(operrateLog.getAgencyId().split(","));
                //遍历集合单位id转成名称
                List<String> agencyNames=new ArrayList<>();
                for (String agencyId : agencyIds) {
                    AgencyInfo agencyInfo = iAgencyInfoDao.getNameById(agencyId);
                    agencyNames.add(agencyInfo.getAgencyName());
                }
                String agencyName= org.apache.commons.lang.StringUtils.join(agencyNames,",");
                operrateLog.setAgencyId(agencyName);
            }
            if(!StringUtils.isEmpty(operrateLog.getPersonId())){
                //将相关人员字符串转化集合
                List<String> personIds = Arrays.asList(operrateLog.getPersonId().split(","));
                //遍历集合人员id转成名称
                List<String> personNames=new ArrayList<>();
                for (String personId : personIds) {
                    String persenName = iPersonInfoDao.getPersonInfoById(personId);
                    personNames.add(persenName);
                }
                String persenName= org.apache.commons.lang.StringUtils.join(personNames,",");
                operrateLog.setPersonId(persenName);
            }
        }
        int total = iOperrateLogsDao.queryOperatelogsTotal(queryParam, childrenId);
        PageDTO<OperrateLogs> pageDTO = new PageDTO<>();
        pageDTO.setRows(operrateLogs);
        pageDTO.setTotal(total);
        return pageDTO;
    }
}
